using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using System.IO.IsolatedStorage;

namespace Risiko.StateManagement
{
  public static class SaveUtils
  {

    private static XmlSerializer serializer;

    public static void SaveGame(State.State state)
    {
      serializer = new XmlSerializer(typeof(State.State));

      IsolatedStorageFile appStorage = IsolatedStorageFile.GetUserStoreForApplication();

      var stream = new StreamWriter(appStorage.OpenFile("savedGame", FileMode.OpenOrCreate));
      serializer.Serialize(stream, state);
      stream.Close();
    }

    public static State.State LoadGame()
    {
      serializer = new XmlSerializer(typeof(State.State));

      IsolatedStorageFile appStorage = IsolatedStorageFile.GetUserStoreForApplication();

      var stream = new StreamReader(appStorage.OpenFile("savedGame", FileMode.OpenOrCreate));
      State.State loadedState = (State.State)serializer.Deserialize(stream);
      stream.Close();
      return loadedState;
    }

  }
}
